package cn.cocho.android.mc.comp.button;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.widget.Button;

import cn.cocho.android.mc.R;
import cn.cocho.android.mc.comp.util.CompUtils;

/**
 * 自定义按钮
 * 
 * @author yzl
 * @time 2013-5-13下午9:03:25
 */
public class McButton extends Button {

	private static final String PRESSED_BACKGROUND = "button_pressedBackground";// 按钮按压时的背景图片
	private static final String DISNABLED_BACKGROUND = "button_disnabledBackground";// 按钮禁用时的背景图片

	private Drawable enableBackground;
	private Drawable pressedBackground;
	private Drawable disnableBackground;

	private AttributeSet attrs;

	private State currentState = State.ENABLE;

	private enum State {
		ENABLE, PRESSED, DISNABLEED
	}
	
	//*
	public McButton(Context context){
		super(context);
	}
	//*
	public McButton(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
	}

	public McButton(Context context, AttributeSet attrs) {
		super(context, attrs);
		this.attrs = attrs;
//		initButton();
		//*↑
		initButton(context);
	}

//	/**
//	 * 初始化按钮
//	 * 
//	 * @author yzl
//	 * @time 2013-5-14下午2:20:44
//	 */
//	public void initButton() {
//		enableBackground = getBackground();
//		pressedBackground = getDrawable(pressedBackground, PRESSED_BACKGROUND);
//		disnableBackground = getDrawable(disnableBackground, DISNABLED_BACKGROUND);
//		if (!isEnabled()) {
//			updateButtonBg(State.DISNABLEED);
//		}
//	}
	
	/**
	 * 初始化按钮
	 * 
	 * @author yzl
	 * @time 2013-5-14下午2:20:44
	 */
	public void initButton(Context context) {
		enableBackground = getBackground();
		TypedArray tdarr = context.obtainStyledAttributes(attrs, R.styleable.McButton);
		pressedBackground = tdarr.getDrawable(R.styleable.McButton_button_pressedBackground);
		disnableBackground = tdarr.getDrawable(R.styleable.McButton_button_disnabledBackground);
		tdarr.recycle();
		if (!isEnabled()) {
			updateButtonBg(State.DISNABLEED);
		}
	}

	@Override
	public boolean onTouchEvent(MotionEvent event) {
		if (!isEnabled()) {
			updateButtonBg(State.DISNABLEED);
			return true;
		}

		switch (event.getAction()) {
		case MotionEvent.ACTION_DOWN:
			updateButtonBg(State.PRESSED);
			break;
		case MotionEvent.ACTION_UP:
			updateButtonBg(State.ENABLE);
			if (CompUtils.isClick(event, this)) {
				performClick();// 如果不加该方法，则在为该按钮添加点击事件时，点击事件失效
			}
		}
		return true;
	}

	/**
	 * 更新控件背景图片
	 * 
	 * @param buttonEvent
	 * @author yzl
	 * @time 2013-5-13下午9:33:31
	 */
	private void updateButtonBg(State state) {
		if (currentState != state && enableBackground != null && pressedBackground != null && disnableBackground != null) {
			if (state == State.PRESSED) {
				setBackgroundDrawable(getPressedBackground());
			} else if (state == State.DISNABLEED) {
				setBackgroundDrawable(getDisnableBackground());
			} else if (state == State.ENABLE) {
				setBackgroundDrawable(enableBackground);
			}
			currentState = state;
		}
	}

	/**
	 * 获得指定属性背景图片对象
	 * 
	 * @param drawable
	 *            背景图片对象
	 * @param eventName
	 *            属性名称
	 * @return
	 * @author HAIKANG SONG
	 * @time 2013-5-14下午4:27:26
	 */
//	private Drawable getDrawable(Drawable drawable, String eventName) {
//		if (drawable == null) {
//			int drawableId = attrs.getAttributeResourceValue(CompUtils.NS_TBC, eventName, 0);// 读取该属性的值
//			if (drawableId > 0) {// 如果有值则使用
//				Resources resources = getResources();
//				drawable = resources.getDrawable(drawableId);
//			} else {// 如果无法获取值，则使用初始背景图片
//				drawable = getBackground();
//			}
//		}
//		return drawable;
//	}

	@Override
	public void setEnabled(boolean enabled) {
		super.setEnabled(enabled);
		if (isEnabled()) {
			updateButtonBg(State.ENABLE);
		} else {
			updateButtonBg(State.DISNABLEED);
		}
	}

	public Drawable getPressedBackground() {
		return pressedBackground;
	}

	public void setPressedBackground(Drawable pressedBackground) {
		this.pressedBackground = pressedBackground;
	}

	public Drawable getDisnableBackground() {
		return disnableBackground;
	}

	public void setDisnableBackground(Drawable disnableBackground) {
		this.disnableBackground = disnableBackground;
	}

}
